import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'hello.html')
    self.response.out.write(template.render(path, template_values))

class DiceHandler(webapp.RequestHandler):
  def get(self):
	#! /usr/bin/python
	# A Python script that simulates rolling two dice a few times.

	# Module for dealing with random numbers.
	import random
	import types

	times = self.request.get('times') 
	while not (times.isdigit()):
		self.response.out.write("INPUT AGAIN<br>") 
		return
		times = self.request.get('times<br>') 

	self.response.out.write("Hello<br>")
	self.response.out.write('Hello'+times+'\n')


	# Number of times to roll the dice.
	DICE_ROLLS = 0

	dice_1 = random.randint(1, 6)
	dice_2 = random.randint(1, 6)


	for  i in range (int(times)):
		DICE_ROLLS = DICE_ROLLS + 1

		self.response.out.write("Rolls:"+str(dice_1)+str(dice_2)+"<br>") 

		if dice_1 == dice_2 and dice_1 == 1:
			self.response.out.write("You Got Snake Eyes<br>") 
		else:
			self.response.out.write("Roll Again<br>") 

		dice_1 = random.randint(1, 6)
		dice_2 = random.randint(1, 6)

		self.response.out.write(str(dice_1) + ' ' + str(dice_2)+'<br>')  
		if dice_1 == dice_2 and dice_1 == 6:
			self.response.out.write("You got Double Sixes<br>") 

class GoodbyeHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write('Goodbye app engine world!')

def main():
  application = webapp.WSGIApplication([('/hello.*', MainHandler),
                                      ('/goodbye.*', GoodbyeHandler),
                                      ('/dice.*', DiceHandler)],
                                     debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()

